package com.etone.smartAudit.utils.word;

import com.etone.smartAudit.utils.DateUtil;
import com.etone.smartAudit.utils.ZipUtil;
import com.etone.smartAudit.vo.auditException.AuditExceptionByteArrayOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author: sovy
 * @date: 2021/2/2
 * @description:
 */
@Component
public class ForeignWordTemplateUtil {

    private static String templateFilePath;

    /**
     * 模板缺省值
     */
    private static String defaultResourceTemplateFilePath = "word/foreignReportTemplate.docx";

    @Value("${audit.word.foreignTemplatePath:}")
    public void setDefaultFilePath(String templatePath) {
        ForeignWordTemplateUtil.templateFilePath = templatePath;
    }

    /**
     * 获取模板文件流
     *
     * @return
     * @throws IOException
     */
    public static InputStream readStream() throws IOException {
        if (StringUtils.isEmpty(templateFilePath)) {
            ClassPathResource classPathResource = new ClassPathResource(defaultResourceTemplateFilePath);
            return classPathResource.getInputStream();
        }
        return new FileInputStream(templateFilePath);
    }

    /**
     * 批量写入流
     *
     * @param datas
     * @return
     */
    public static List<AuditExceptionByteArrayOutputStream> batchWriteStream(List<Map<String, Object>> datas) {
        InputStream inputStream = null;
        XWPFDocument xwpfDocument = null;
        ByteArrayOutputStream destOutStream = null;
        List<AuditExceptionByteArrayOutputStream> outputStreams = new ArrayList<>();
        int i = 0;
        try {
            for (Map<String, Object> params : datas) {
                try {
                    inputStream = readStream();
                    xwpfDocument = new XWPFDocument(inputStream);

                    AuditExceptionByteArrayOutputStream outputStream = new AuditExceptionByteArrayOutputStream();
                    outputStream.setFileName(params.get("reportStartTime") + "-" + params.get("reportEndTime") + params.get("city") + "风险扫描报告" + ".docx");
                    ForeignWordUtil foreignWordUtil = new ForeignWordUtil(xwpfDocument, params);
                    foreignWordUtil.create();
                    xwpfDocument.write(outputStream);
                    outputStreams.add(outputStream);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (xwpfDocument != null) {
                        try {
                            xwpfDocument.close();
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                }

            }
        } catch (Exception ioException) {
            ioException.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }
            if (destOutStream != null) {
                try {
                    destOutStream.close();
                } catch (IOException ioException) {
                    ioException.printStackTrace();
                }
            }

        }

        return outputStreams;
    }

    /**
     * 导出
     *
     * @param datas
     * @param response
     */
    public static void export(List<Map<String, Object>> datas, HttpServletResponse response) {

        List<AuditExceptionByteArrayOutputStream> outputStreams = batchWriteStream(datas);
        ZipUtil.ioToZip(outputStreams, "地市对外风险扫描报告" + DateUtil.parseDate2String(new Date(), "yyyyMMddHHmmss") + ".zip", response);
    }

}
